package com.party.web.biz.copartner;

import com.party.common.constant.Constant;
import com.party.common.utils.DateUtils;
import com.party.core.model.distributor.DistributorDetail;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.distributor.DistributorRelationType;
import com.party.core.service.distributor.IDistributorDetailService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.core.service.member.IMemberCopartnerCountService;
import com.party.core.service.member.IMemberCopartnerDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 众筹分析业务接口
 * Created by wei.li
 *
 * @date 2017/7/10 0010
 * @time 15:04
 */
@Service
public class MemberCopartnerDetailBizService {

    @Autowired
    private IDistributorDetailService distributorDetailService;

    @Autowired
    private IDistributorRelationService distributorRelationService;

    @Autowired
    private IMemberCopartnerDetailService memberCopartnerDetailService;

    @Autowired
    private IMemberCopartnerCountService memberCopartnerCountService;


    /**
     * 更新报名数
     *
     * @param projectId 众筹编号
     */
    public void updateApplyNum(String projectId) {
        DistributorDetail distributorDetail = distributorDetailService.findByTargetId(projectId);
        if (null != distributorDetail) {
            DistributorRelation distributorRelation = distributorRelationService.get(distributorDetail.getDistributorRelationId());
            if (null != distributorRelation
                    && (DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode().equals(distributorRelation.getType()))
                    || DistributorRelationType.COURSE_DISTRIBUTION.getCode().equals(distributorRelation.getType())) {
                String distributorId = distributorRelation.getDistributorId();

                memberCopartnerCountService.updateApplyNum(projectId, distributorId);

                memberCopartnerDetailService.updateApplyNum(projectId, distributorId);
            }
        }
    }

    /**
     * 更新报名数
     *
     * @param distributorRelation 分销关系
     * @param createDate          创建时间
     */
    @Transactional
    public void updateApplyNumSub(DistributorRelation distributorRelation, Date createDate) {
        if (null != distributorRelation && (
                DistributorRelationType.COURSE_DISTRIBUTION.getCode().equals(distributorRelation.getType())
                        || DistributorRelationType.CROWDFUND_DISTRIBUTION.getCode().equals(distributorRelation.getType()))) {

            String formatDate = DateUtils.formatDate(createDate, DateUtils.DATE_PATTERN);
            String formatToday = DateUtils.formatDate(new Date(), DateUtils.DATE_PATTERN);
            String option = formatToday.equals(formatDate) ? Constant.OPTION_ADD : Constant.OPTION_SUB;

            memberCopartnerCountService.updateApplyRefund(distributorRelation.getDistributorId(), 0d);
            memberCopartnerDetailService.updateApplyRefund(distributorRelation.getDistributorId(), 0d, option);
        }
    }
    public void updateBrokerage(String memberId, Double brokerage, Boolean isContain, boolean isBegin, double withdrawalTotal) {
        memberCopartnerCountService.updateBrokerage(memberId, brokerage, isContain, isBegin, withdrawalTotal);

        memberCopartnerDetailService.updateBrokerage(memberId, brokerage, isContain, isBegin, withdrawalTotal);
    }
}
